package com.hmkj.quote.queue;

import com.hmkj.boot.utils.SpringContextUtils;
import com.hmkj.core.constant.CacheID;
import com.hmkj.core.po.follow.FollowRule;
import com.hmkj.core.po.order.Order;
import com.hmkj.core.po.order.OrderRelation;
import com.hmkj.core.po.order.OrderSimulate;
import com.hmkj.core.service.cache.RedisService;
import com.hmkj.core.service.follow.FollowRuleService;
import com.hmkj.core.service.order.OrderRelationService;
import com.hmkj.core.service.order.OrderService;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * 操盘手处理跟单队列线程
 *
 * @author eproo
 * @date 2017年03月16日 21:38
 */
@Slf4j
public class TraderQueueThread implements Runnable {


    private RedisService redisService = SpringContextUtils.getBean(RedisService.class);
    private FollowRuleService followRuleService = SpringContextUtils.getBean(FollowRuleService.class);
    private OrderService orderService = SpringContextUtils.getBean(OrderService.class);
    private OrderRelationService orderRelationService = SpringContextUtils.getBean(OrderRelationService.class);

    @Override
    public void run() {
        while (true) {
            try {
                if (startQueue()) {
                    Thread.sleep(1000);
                }
            } catch (Exception e) {
                log.error(e.getMessage());
                e.printStackTrace();
            }
        }
    }

    private boolean startQueue() throws Exception {
        boolean isFinish = false;
        try {
            OrderSimulate orderSimulate = redisService.pop(CacheID.QUEUE_FOLLOW,OrderSimulate.class);
            if (null == orderSimulate) {
                return true;
            }
            log.info("处理操盘手模拟订单跟单，模拟订单号："+orderSimulate.getOrderNo());
            List<FollowRule> followRules = followRuleService.select(new FollowRule(f ->{
                f.setFollowId(orderSimulate.getUserId());
                f.setStatus(FollowRule.STATUS.T1.code);
                f.setDelFlag(FollowRule.DELFLAG.NORMAL.code);
            }));
            followRules.forEach(followRule -> {
                try {
                    //生成实盘订单
                    Integer orderId = orderService.doTrade(followRule.getUserId(), orderSimulate.getCommoId(), followRule.getNumber(), orderSimulate.getLoseStopPoint(), 0, orderSimulate.getOpenWay(), orderSimulate.getDirection(),orderSimulate.getEntrustPrice(), orderSimulate.getResource(), Order.FOLLOW.T2.code,followRule.getFollowId(),"0.0.0.0");
                    Order order = orderService.selectByPrimaryKey(orderId);
                    //加入对应模拟订单17121255664957
                    orderRelationService.insert(new OrderRelation(orderRelation -> {
                        orderRelation.setSimId(orderSimulate.getId());
                        orderRelation.setRealOrderNo(order.getOrderNo().substring(0,14));
                    }));
                } catch (Exception e) {
                    log.error("跟单失败，错误消息：" + e.getMessage());
                    e.printStackTrace();
                }
            });
        } catch (Exception e) {
            log.error("执行操盘手处理跟单队列失败：" + e.getMessage());
            e.printStackTrace();
        }
        return isFinish;
    }
}
